#!/usr/bin/env python
#-*- coding:utf-8 -*-

"""
	PROJECT pyevocomm -

	APP		Sochet server Speech

	MODULE	Text to speech

	SITE DESCRIPTION http://code.google.com/p/pyevocomm/ 

	@copyright: 2012 by archetipo <alessio.gerace@gmail.com>
	@license: New BSD License, see COPYING for details.
"""

from socket import *
import json
import pyttsx 
#import sServer as Tts
print 'Inizzializzazione server e text to speech'
engine = pyttsx.init() 
v=engine.getProperty('voices')
engine.setProperty('voice', v[4].id)
engine.setProperty('rate', 150)
porta = 4000
server = socket(AF_INET, SOCK_STREAM)
server.bind(('19.61.1.4', porta))
server.listen(5)
server.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
print 'server in ascolto sulla porta 4000 pronto...'
while True:
  print 'attesa..'
  connection, address = server.accept()
  data = connection.recv(1024)
  j=json.loads(data)
  if j['comm']=='stat':
    if j['dat']=='stop':
      message = "ok"
      connection.send(message)
      break
  elif j['comm']=='play':
    print 'ricevuto'
    engine.say(j['dat'])
    engine.runAndWait()
    message = "ok"
    connection.send(message)
print 'connessione chiusa dal client'
connection.close()
